#include "checkuserwidget.h"

CheckUserWidget::CheckUserWidget(QWidget *parent) : QWidget(parent)
{
         init_Widget();
         init_Control();
         init_Connect();
}
void CheckUserWidget::init_Control()
{
    //创建表格
    this->table=new QTableWidget(this);
    table->setRowCount(0);
    //this->table->setRowCount(2);
    this->table->setColumnCount(5);
    this->table->resize(800,500);
    //qDebug()<<"执行CheckInfomation";
    this->CheckInfomation();
    this->table->setHorizontalHeaderItem(0,new QTableWidgetItem("UserId"));
    this->table->setHorizontalHeaderItem(1,new QTableWidgetItem("姓名"));
    this->table->setHorizontalHeaderItem(2,new QTableWidgetItem("职位"));
    this->table->setHorizontalHeaderItem(3,new QTableWidgetItem("账号"));
    this->table->setHorizontalHeaderItem(4,new QTableWidgetItem("密码"));
    this->table->setEditTriggers(QAbstractItemView::NoEditTriggers);

    //this->table->setItem(0,0,new QTableWidgetItem("nini"));
    //this->table->setItem(0,1,new QTableWidgetItem("1"));
    //this->table->setItem(1,0,new QTableWidgetItem("ni"));
    //this->table->setItem(1,1,new QTableWidgetItem("2"));

    //创建按钮
    this->returnbtn=new QPushButton(this);
    this->returnbtn->setText(QString::fromUtf8("返回"));
    this->returnbtn->setMaximumSize(100,70);


    this->lay=new QVBoxLayout(this);
    this->lay->addWidget(this->table);
    this->lay->addWidget(returnbtn);

}

void CheckUserWidget::init_Widget()
{
    this->setWindowTitle(QString::fromUtf8("管理窗口"));
    this->resize(800,600);

}

void CheckUserWidget::init_Connect()
{
  connect(this->returnbtn,SIGNAL(clicked()),this,SLOT(on_backBtn_cliked()));
}

void CheckUserWidget::CheckInfomation()
{
    //qDebug()<<"执行CheckInfomation";
    QString sql=QString("SELECT *FROM Users ;");
    int j=0,i=0;
    adduser.resize(100);//初始化
    //adduser.fill(nullptr);
    this->model=this->control->getInstance()->Creatmodel("user");
    this->adduser=this->model->getUser((char*)sql.toStdString().c_str());//将员工数据从数据库拉出来
    //qDebug()<<adduser.capacity();
    this->table->setRowCount(this->model->getRow((char*)sql.toStdString().c_str()));//设置表格行数
    for(i;i<this->adduser.size();i++)
    {
       j=0;
        //qDebug()<<"运行插入表数据";
       QString user_identity="普通用户";
        //将int型转换成string型
        QString userIdStr = QString::number(this->adduser[i].getUserId());
        //判断职位
        if(this->adduser[i].getUser_identity()==0)
        {
             user_identity="普通用户";
        }
        else if(this->adduser[i].getUser_identity()==1)
        {
            user_identity="管理员";
        }





        //将所有用户信息放入表中
        this->table->setItem(i,j++,new QTableWidgetItem(userIdStr));
        this->table->setItem(i,j++,new QTableWidgetItem(this->adduser[i].getUsername()));
        this->table->setItem(i,j++,new QTableWidgetItem(user_identity));
        this->table->setItem(i,j++,new QTableWidgetItem(this->adduser[i].getLoginname()));
        this->table->setItem(i,j++,new QTableWidgetItem(this->adduser[i].getUserPassword()));


    }



}

void CheckUserWidget::on_backBtn_cliked()
{
    //qDebug()<<"发出信号";
    emit returnWidget();

}
